158C - Cd and pwd commands - CodeForces Solution


*special problem data structures implementation *1400

Please click on ads to support us..

Python Code:

n = int(input())
start = []
for _ in range(n):
    x = input().split()
    if x[0] == 'pwd':
        print(*(['/'] + start), sep='')
    else:
        m = x[1].split('/')
        if m[0] == "":
            m = m[1:]
            start = []
        for i in m:
            if i == '..':
                start.pop()
            else:
                start.append(i + '/')

C++ Code:

#include <iostream>
#include <vector>
#include <string>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
using namespace std;
#define ull unsigned long long
#define ll signed long long
#define ld long double
#define to_low(s) transform(s.begin(), s.end(), s.begin(), ::tolower); // convert string to lowercase
#define to_up(s) transform(s.begin(), s.end(), s.begin(), ::toupper);  // convert string to uppercase
/*------------------------------------------------------------------------------------------------------------------------------*/
vector<string> vs;
void pwd()
{
    cout << "/";
    for (vector<string>::iterator it = vs.begin(); it != vs.end(); it++)
        cout << *it << "/";
    cout << endl;
}
void cd(char path[])
{
    if (path[0] == '/')
    {
        vs.clear();
        path++;
    }
    path = strtok(path, "/");
    while (path)
    {
        string t = path;
        if (t == "..")
            vs.pop_back();
        else
            vs.push_back(t);
        path = strtok(0, "/");
    }
}
/*------------------------------------------------------------------------------------------------------------------------------*/
int main(void)
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    char cmd[5], path[250];
    for (int i = 0; i < n; ++i)
    {
        cin >> cmd;
        if (strcmp(cmd, "pwd") == 0)
            pwd();
        else
        {
            cin >> path;
            cd(path);
        }
    }
        
    return 0;
}


Comments

Submit
0 Comments
More Questions

1476E - Pattern Matching
1107A - Digits Sequence Dividing
1348A - Phoenix and Balance
1343B - Balanced Array
1186A - Vus the Cossack and a Contest
1494A - ABC String
1606A - AB Balance
1658C - Shinju and the Lost Permutation
1547C - Pair Programming
550A - Two Substrings
797B - Odd sum
1093A - Dice Rolling
1360B - Honest Coach
1399C - Boats Competition
1609C - Complex Market Analysis
1657E - Star MST
1143B - Nirvana
1285A - Mezo Playing Zoma
919B - Perfect Number
894A - QAQ
1551A - Polycarp and Coins
313A - Ilya and Bank Account
1469A - Regular Bracket Sequence
919C - Seat Arrangements
1634A - Reverse and Concatenate
1619C - Wrong Addition
1437A - Marketing Scheme
1473B - String LCM
1374A - Required Remainder
1265E - Beautiful Mirrors